package com.scvtc.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

public class WebUtil {

    private WebUtil() {
    }

    private final static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 将对象转为 json 返回给前端
     *
     * @param response
     * @param object
     */
    public static void resultJson(HttpServletResponse response, Object object) {
        try (ServletOutputStream writer = response.getOutputStream()) {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            String json = objectMapper.writeValueAsString(object);
            writer.write(json.getBytes());
            writer.flush();
        } catch (Exception e) {
            e.getStackTrace();
        }
    }
}